package com.mvpandroiddemo.home;


import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.mvpandroiddemo.R;
import com.mvpandroiddemo.base.BaseActivity;
import com.mvpandroiddemo.fragment.main.MainFragment;
import com.mvpandroiddemo.fragment.project.ProjectFragment;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;


public class HomeActivity extends BaseActivity {

    @BindView(R.id.home_content_vp)
    ViewPager ContentVp;

    @BindView(R.id.home_bottom_navigation_bnv)
    BottomNavigationView bottomNavigationBnv;

    private HomePresenter homePresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
        homePresenter = new HomePresenter(new HomeModel());
        homePresenter.attachView(this);
        initView();
    }

    private void initView() {
        ArrayList<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            if (i % 2 == 0) {
                MainFragment mainFragment = new MainFragment();
                fragments.add(mainFragment);
            } else {
                fragments.add(new ProjectFragment());
            }
        }
        HomeVpAdapter homeVpAdapter = new HomeVpAdapter(getSupportFragmentManager());
        homeVpAdapter.setFragments(fragments);
        ContentVp.setAdapter(homeVpAdapter);

        bottomNavigationBnv.setOnNavigationItemSelectedListener(menuItem -> {
            int showItem = 0;
            switch (menuItem.getItemId()) {
                case R.id.navigation_home:
                    showItem = 0;
                    break;
                case R.id.navigation_project:
                    showItem = 1;
                    break;
                case R.id.navigation_square:
                    showItem = 2;
                    break;
                case R.id.navigation_public_number:
                    showItem = 3;
                    break;
                case R.id.navigation_mine:
                    showItem = 4;
                    break;
            }
            ContentVp.setCurrentItem(showItem);
            return true;
        });

        ContentVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                bottomNavigationBnv.getMenu().getItem(position).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (homePresenter != null) {
            homePresenter.detachView();
        }
    }
}
